// mircoApp/userAccount/accountIndex/accountIndex.js
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [skinBehavior],
  data: {
    userForm:{
      type:'1',
      id_card_type:'',
      id_card_no:'',
      id_card_valid_type:'',
      id_card_valid_start_time:'',
      id_card_valid_end_time:'',
      name:'',
      mobile:'',
    },
    iPnoneBottomBol:false, //是否是苹果手机
    cardTypeName:'',
    cardValidName:'',
    cardTypeIndex:0,
    cardValidIndex:0,
    cardIndex:0,
    cardTypeShow:false,
    dateShow:false,
    cardTitle:'',
    cardType:'',
    cardTimeType:'',
    cardArr:[],
    succeedRadio:'1',
    cardTypeArr:[
      {
        name:'身份证',
        value:17
      },
      {
        name:'护照',
        value:18
      },
      {
        name:'港澳居民来往内地通行证',
        value:19
      },
      {
        name:'台湾居民来往内地通行证',
        value:20
      }
    ],
    cardValidArr:[
      {
        name:'长期有效',
        value:1
      },
      {
        name:'非长期有效',
        value:0
      }
    ],
    currentDate:new Date().getTime(),
    minDate: new Date(1970, 0, 1).getTime(),
    maxDate: new Date(2050, 12, 1).getTime(),
    startTime:'',
    endTime:'',
    withdrawInfo:{},
    //修改还是提交的状态码
    btnStatus:0,//0就是未提交，1是开户成功，2是开户成功修改中
    formatter(type, val) {
      if (type === 'year') {
        return val + '年';
      }
      if (type === 'month') {
        return val + '月';
      }
      if (type === 'day') {
        return val + '日';
      }
      return val;
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.withdrawData();
  },
  modelInput(event) {
    const item = event.currentTarget.dataset.item,
          itemValue = event.detail.value;
    this.setData({
      [item]:itemValue
    })
  },
  //获取结算，设置银行卡数据
  withdrawData() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.integration-pay-share.frontend.withdraw.index");
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data; //没有数据返回的是一个空数组
          if(!Array.isArray(data)) {
            this.setData({
              withdrawInfo:data,
              'userForm.mobile':data.mobile,
              'userForm.name':data.name,
              'userForm.id_card_no':data.id_card_no,
              'userForm.id_card_type':data.id_card_type,
              'userForm.id_card_valid_type':data.id_card_valid_type,
              'userForm.id_card_valid_start_time':data.id_card_valid_start_time,
              'userForm.id_card_valid_end_time':data.id_card_valid_end_time,
              cardValidName:data.id_card_valid_type_name,
              cardTypeName:data.id_card_type_name,
              startTime:(data.id_card_valid_start_time_stamp * 1000),
              endTime:(data.id_card_valid_end_time_stamp * 1000)
            })
            if(data.id) {
              this.setData({
                btnStatus:1
              })
            }
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  cardTypeOn(event) {
    if(this.data.btnStatus == 1) return false;
    let {name} = event.currentTarget.dataset;
    if(name == 'type') {
      this.data.cardTitle = '个人证件类型';
      this.data.cardArr = this.data.cardTypeArr;
      this.data.cardIndex = this.data.cardTypeIndex;
    } else {
      this.data.cardTitle = '证件有效期类型';
      this.data.cardArr = this.data.cardValidArr;
      this.data.cardIndex = this.data.cardValidIndex;
    }
    if((this.data.btnStatus == 2 && name != 'type') || this.data.btnStatus == 0) {
      this.data.cardTypeShow = true;
    }
    this.setData({
      cardType:name,
      cardTypeShow:this.data.cardTypeShow,
      cardTitle:this.data.cardTitle,
      cardArr:this.data.cardArr,
      cardIndex:this.data.cardIndex
    })
  },
  cardConfrim(event) {
    let {cardIndex} = event.detail;
    if(this.data.cardType == 'type') {
      this.data.userForm.id_card_type = this.data.cardTypeArr[cardIndex].value;
      this.data.cardTypeName = this.data.cardTypeArr[cardIndex].name;
      this.data.cardTypeIndex= cardIndex;
    } else {
      this.data.userForm.id_card_valid_type = this.data.cardValidArr[cardIndex].value;
      this.data.cardValidName =  this.data.cardValidArr[cardIndex].name;
      this.data.cardValidIndex= cardIndex;
    }
    this.setData({
      'userForm.id_card_type':this.data.userForm.id_card_type,
      'userForm.id_card_valid_type':this.data.userForm.id_card_valid_type,
      cardTypeName:this.data.cardTypeName,
      cardTypeIndex:this.data.cardTypeIndex,
      cardValidName:this.data.cardValidName,
      cardValidIndex:this.data.cardValidIndex
    })
  },
  dateCancel() {
    this.setData({
      dateShow:false
    })
  },
  dateConfirm(event) {
    if(this.data.cardTimeType == 'start') {
      this.data.userForm.id_card_valid_start_time = this.timestampToTime(event.detail);
      this.data.startTime = event.detail;
    } else {
      this.data.userForm.id_card_valid_end_time = this.timestampToTime(event.detail);
      this.data.endTime = event.detail;
    }
    this.setData({
      dateShow:false,
      endTime:this.data.endTime,
      startTime:this.data.startTime,
      'userForm.id_card_valid_start_time':this.data.userForm.id_card_valid_start_time,
      'userForm.id_card_valid_end_time': this.data.userForm.id_card_valid_end_time
    })
  },
  windEdit() {
    this.setData({
      btnStatus:2
    })
  },
  accountSubmit() {
    if(app._isTextEmpty(this.data.cardTypeName)) {
      wx.showToast({
        icon: 'none',
        title: '请选择个人证件类型',
        duration: 1500
      });
      return;
    }
    if(app._isTextEmpty(this.data.userForm.id_card_no)) {
      wx.showToast({
        icon: 'none',
        title: '请输入个人证件号码',
        duration: 1500
      });
      return;
    }
    if(app._isTextEmpty(this.data.userForm.id_card_valid_start_time)) {
      wx.showToast({
        icon: 'none',
        title: '请选择个人证件有效期开始日期',
        duration: 1500
      });
      return;
    }
    if(app._isTextEmpty(this.data.userForm.id_card_valid_end_time)) {
      wx.showToast({
        icon: 'none',
        title: '请选择个人证件有效期截止日期',
        duration: 1500
      });
      return;
    }
    if(app._isTextEmpty(this.data.userForm.name)) {
      wx.showToast({
        icon: 'none',
        title: '请输入个人姓名',
        duration: 1500
      });
      return;
    }
    if(app._isTextEmpty(this.data.userForm.mobile)) {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号',
        duration: 1500
      });
      return;
    }
    if(this.data.startTime >= this.data.endTime 
      && ( this.data.userForm.id_card_valid_type != 1  || this.data.userForm.id_card_type != 17)) {
      wx.showToast({
        icon: 'none',
        title: '个人证件有效期开始日期不能大于截至日期',
        duration: 1500
      });
      return;
    }
    if(this.data.withdrawInfo?.id && this.data.btnStatus == 2) {
      this.editAccount();
    } else {
      this.openAccount();
    }
  },
  openAccount() {
    wx.showLoading({
      title: '加载中',
    })
    let urlStr = app.getNetAddresss("plugin.integration-pay-share.frontend.withdraw.open-account");
    app._postNetWork({
      url: urlStr,
      data:{form:this.data.userForm},
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data; //没有数据返回的是一个空数组
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
          wx.hideLoading();
          this.withdrawData();
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  editAccount() {
    let json = {
      id_card_valid_type:this.data.userForm.id_card_valid_type,
      id_card_valid_start_time:this.data.userForm.id_card_valid_start_time,
      id_card_valid_end_time:this.data.userForm.id_card_valid_end_time,
      mobile:this.data.userForm.mobile,
    }
    wx.showLoading({
      title: '加载中',
    })
    let urlStr = app.getNetAddresss("plugin.integration-pay-share.frontend.withdraw.edit-account");
    app._postNetWork({
      url: urlStr,
      data:{form:json},
      success:(resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let data = res.data; //没有数据返回的是一个空数组
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
          wx.hideLoading();
          this.withdrawData();
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  cardStartTime(event) {
    let {name} = event.currentTarget.dataset;
    if(this.data.btnStatus == 1) return false;
    this.setData({
      cardTimeType:name,
      dateShow:true
    })
  },
  windAccountTo() {
    wx.navigateTo({
      url:'/mircoApp/userAccount/windAccount/windAccount'
    })
  },
  //日期转化
  timestampToTime(timestamp) {
    var date = new Date(timestamp);
    let Y = date.getFullYear() + "-";
    let M =
      (date.getMonth() + 1 < 10 ?
        "0" + (date.getMonth() + 1) :
        date.getMonth() + 1) + "-";
    let D = this.change(date.getDate()) + " ";
    return Y + M + D;
  },
  change(t) {
    if (t < 10) {
      return "0" + t;
    } else {
      return t;
    }
  },
  getiPnoneBottom() {
    let that = this;
    try {
      let value = wx.getStorageSync('iPhoneBottom');
      if (value) {
        that.setData({
          iPnoneBottomBol: true
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})